//
//  nextXianXiaHuoDongViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/19.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "nextXianXiaHuoDongViewController.h"
#import "HttpRequest.h"
#import "allActivity.h"
#import "baseTableViewCell.h"
#import "Macros.h"
#import "UIImageView+WebCache.h"
#import "xianXiaHuoDongViewController.h"
#import "xianXiaDetailViewController.h"

#import "SVProgressHUD.h"

@interface nextXianXiaHuoDongViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UIView *headerView02;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property(nonatomic,copy)NSMutableArray *json_Array;
@property(nonatomic,retain)baseTableViewCell*myCell;
@property(nonatomic,copy)NSMutableArray *idArray;

@end

@implementation nextXianXiaHuoDongViewController
@synthesize json_Array,myCell,idArray;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
static float progress = 0.0f;

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"111111111");
    idArray=[NSMutableArray array];

    //设置背景色
    [self setBackGroundColor];
    //获取数据
    [self getDataFromNet];
    progress=0.0f;
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.myTableView.hidden=YES;
    
}
//设置背景色
-(void)setBackGroundColor
{
    self.headerView01.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView02.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
//    self.myTableView.delegate=self;
//    self.myTableView.dataSource=self;
}
//从服务器获取数据
-(void)getDataFromNet
{
    allActivity* aa=[allActivity shardeManager];
    NSString*urlString=aa.addressString;
    self.titleLabel.text=aa.titleString;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:nil success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         json_Array=[json objectForKey:@"data"];
         [self dismiss];
//         [self.myTableView reloadData];
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)increaseProgress
{
    progress += 0.05f;
    [SVProgressHUD showProgress:progress status:@"加载中..."];
    
    if(progress < 1.0f){
        [self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.1f];
    } else {
        [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.4f];
    }
}
- (void)dismiss
{
    self.myTableView.hidden=NO;
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [SVProgressHUD dismiss];
    [self.myTableView reloadData];

}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return json_Array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    allActivity *aa=[allActivity shardeManager];
    myCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (myCell == nil)
    {
        myCell = [[[NSBundle mainBundle] loadNibNamed:@"baseTableViewCell" owner:self options:nil]lastObject];
        myCell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (json_Array.count>0)
    {
        aa.contentString=[json_Array[indexPath.row] objectForKey:@"content"];
        aa.picString=[json_Array[indexPath.row] objectForKey:@"pic"];
        aa.sellerString=[json_Array[indexPath.row] objectForKey:@"seller"];
        aa.tpriceString=[json_Array[indexPath.row] objectForKey:@"tprice"];
        aa.idString=[json_Array[indexPath.row] objectForKey:@"id"];
        [idArray addObject:aa.idString];
        myCell.nameLabel.text=aa.sellerString;
        myCell.tuanGou.textColor=pinkColor;
        myCell.contentLabel.text=aa.contentString;
        myCell.priceLabel.textColor=pinkColor;
        myCell.priceLabel.text=[NSString stringWithFormat:@"¥%@",aa.tpriceString];
    }
    NSURL* imagePath = [NSURL URLWithString:aa.picString];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    return myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return myCell.frame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *idString=idArray[indexPath.row];
    allActivity *aa=[allActivity shardeManager];
    aa.idString=idString;
    aa.num=10;
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    NSString *num=[NSString stringWithFormat:@"%d",aa.num];
    [userDefaults setObject:num forKey:@"aa.num"];
//    NSLog(@"num=%@",num);
    xianXiaDetailViewController *xxdv=[xianXiaDetailViewController new];
    [self.navigationController pushViewController:xxdv animated:YES];



}
- (IBAction)goBack:(id)sender
{
    xianXiaHuoDongViewController *xxhd=self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:xxhd animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
